<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="客户唯一标识" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户唯一标识"></el-input>
                </el-form-item>
                <el-form-item label="客户姓名" prop="customerName">
                        <el-input v-model="dataForm.customerName" placeholder="客户姓名"></el-input>
                </el-form-item>
                <el-form-item label="客户邮箱" prop="email">
                        <el-input v-model="dataForm.email" placeholder="客户邮箱"></el-input>
                </el-form-item>
                <el-form-item label="客户电话号码" prop="phone">
                        <el-input v-model="dataForm.phone" placeholder="客户电话号码"></el-input>
                </el-form-item>
                <el-form-item label="客户注册日期" prop="registrationDate">
                        <el-date-picker
                                v-model="dataForm.registrationDate"
                                type="datetime"
                                placeholder="客户注册日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="客户最后一次购买日期" prop="lastPurchaseDate">
                        <el-date-picker
                                v-model="dataForm.lastPurchaseDate"
                                type="datetime"
                                placeholder="客户最后一次购买日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="客户总消费金额" prop="totalSpent">
                        <el-input v-model="dataForm.totalSpent" placeholder="客户总消费金额"></el-input>
                </el-form-item>
                <el-form-item label="购买频率" prop="purchaseFrequency">
                        <el-input v-model.number="dataForm.purchaseFrequency" placeholder="购买频率"></el-input>
                </el-form-item>
                <el-form-item label="平均购买金额" prop="averagePurchaseAmount">
                        <el-input v-model="dataForm.averagePurchaseAmount" placeholder="平均购买金额"></el-input>
                </el-form-item>
                <el-form-item label="客户生命周期价值" prop="customerLifecycleValue">
                        <el-input v-model="dataForm.customerLifecycleValue" placeholder="客户生命周期价值"></el-input>
                </el-form-item>
                <el-form-item label="是否为活跃客户" prop="isActive">
                        <el-input v-model="dataForm.isActive" placeholder="是否为活跃客户"></el-input>
                </el-form-item>
                <el-form-item label="流失原因（如果流失）" prop="churnReason">
                        <el-input v-model="dataForm.churnReason" placeholder="流失原因（如果流失）"></el-input>
                </el-form-item>
                <el-form-item label="流失日期（如果流失）" prop="churnDate">
                        <el-date-picker
                                v-model="dataForm.churnDate"
                                type="datetime"
                                placeholder="流失日期（如果流失）">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="客户分层（如高价值客户、中价值客户等）" prop="segment">
                        <el-input v-model="dataForm.segment" placeholder="客户分层（如高价值客户、中价值客户等）"></el-input>
                </el-form-item>
                <el-form-item label="记录创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="记录创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="记录更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="记录更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="客户偏好（如产品偏好、购买渠道偏好等）" prop="preferences">
                        <el-input v-model="dataForm.preferences" placeholder="客户偏好（如产品偏好、购买渠道偏好等）"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    customerId: null,
                    customerName: null,
                    email: null,
                    phone: null,
                    registrationDate: null,
                    lastPurchaseDate: null,
                    totalSpent: null,
                    purchaseFrequency: null,
                    averagePurchaseAmount: null,
                    customerLifecycleValue: null,
                    isActive: null,
                    churnReason: null,
                    churnDate: null,
                    segment: null,
                    createdAt: null,
                    updatedAt: null,
                    preferences: null,
                },
                dataRule: {
                    customerId: [
                        { required: true, message: '客户唯一标识不能为空', trigger: 'blur' }
                    ],
                    customerName: [
                        { required: true, message: '客户姓名不能为空', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '客户邮箱不能为空', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '客户电话号码不能为空', trigger: 'blur' }
                    ],
                    registrationDate: [
                        { required: true, message: '客户注册日期不能为空', trigger: 'blur' }
                    ],
                    lastPurchaseDate: [
                        { required: true, message: '客户最后一次购买日期不能为空', trigger: 'blur' }
                    ],
                    totalSpent: [
                        { required: true, message: '客户总消费金额不能为空', trigger: 'blur' }
                    ],
                    purchaseFrequency: [
                        { required: true, message: '购买频率不能为空', trigger: 'blur' }
                    ],
                    averagePurchaseAmount: [
                        { required: true, message: '平均购买金额不能为空', trigger: 'blur' }
                    ],
                    customerLifecycleValue: [
                        { required: true, message: '客户生命周期价值不能为空', trigger: 'blur' }
                    ],
                    isActive: [
                        { required: true, message: '是否为活跃客户不能为空', trigger: 'blur' }
                    ],
                    churnReason: [
                        { required: true, message: '流失原因（如果流失）不能为空', trigger: 'blur' }
                    ],
                    churnDate: [
                        { required: true, message: '流失日期（如果流失）不能为空', trigger: 'blur' }
                    ],
                    segment: [
                        { required: true, message: '客户分层（如高价值客户、中价值客户等）不能为空', trigger: 'blur' }
                    ],
                    createdAt: [
                        { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
                    ],
                    updatedAt: [
                        { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
                    ],
                    preferences: [
                        { required: true, message: '客户偏好（如产品偏好、购买渠道偏好等）不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppCustomer/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.appcustomer
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppCustomer/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>